<?php

/**
 * @author liyi
 * @copyright 2018.12.10
 */
namespace Paolgame\Controller;
use Common\Controller\ApiController;
use Think\Model;
use Think\Log;

// 泡泡龙游戏
class LoginController extends ApiController {

    const GAME_TYPE = 'Paolgame';  // 游戏类型

    const DB_USER_NAME = 'paol_game.users'; // 游戏数据表

    const TOURIST = "paol:tourist:sid:%"; // 游戏游客用户 - redis

    const AUTH_USER = "paol:auth_user:sid:%";  // 授权用户

    const CACHE_KEY = 'paol';  // 游戏全局缓存键

    const PAOL_COUNT_KEY = "paol:count"; // 渠道来源统计缓存键

    const PAOL_DBID = 3; // 邀请次数 redis数据库

    // 登陆授权接口
    public function login()
    {
        $gameType = self::GAME_TYPE;
        $nickname = I('nickname','','string');
        $avatar = I('avatar','','string');
        $id = I('id','','int');
        $openid = I('openid','','string');
        $sid = I('source',0,'int');

        $data['openid'] = $openid;
        $data['nickname'] = $nickname;
        $data['avatar'] = $avatar;
        $data['id'] = $id;

        $dbname = self::DB_USER_NAME;
        $cacheKey = self::CACHE_KEY;
        $cacheval = sprintf(self::AUTH_USER,$sid);

        $dbId = self::PAOL_DBID;

        $userInfo = $this->commLogin_ppl($dbname,$data,$cacheKey,$cacheval,$dbId);
        $userInfo['serverTime'] = time();
        $incr = $userInfo['incr'];
        $this->getChannelCount(self::PAOL_COUNT_KEY,$sid,'auth_user',$incr,$dbId);

        $this->response(0,'ok',$userInfo);
    }

    //未授权登陆接口
    public function loginNotAuth() {
        $openid = I('post.openid');
        $code = I('code','','string');
        $sid = I('source',0,'int');
        $z_index = I('z_index',0,'int');

        $gameType = self::GAME_TYPE;// 游戏类型 Paolgame
        $dbname = self::DB_USER_NAME;// 游戏数据表
        $cacheKey = self::CACHE_KEY;// 游戏全局缓存键
        $cacheval = sprintf(self::TOURIST,$sid);// 游戏游客用户
        $dbId = self::PAOL_DBID;// 缓存数据编号
        if($openid){
            $userInfo = $this->commLoginNotAuth_pplx($gameType,$dbname,$code,$cacheKey,$cacheval,$dbId,$sid,$z_index,$openid);
        }else{
            $userInfo = $this->commLoginNotAuth_ppl($gameType,$dbname,$code,$cacheKey,$cacheval,$dbId,$sid,$z_index);
        }
        $userInfo['serverTime'] = time();
        $incr = $userInfo['incr'];
        $this->getChannelCount(self::PAOL_COUNT_KEY,$sid,'tourist',$incr,$dbId);

        $this->response(0,'ok',$userInfo);
    }
}